#!/bin/csh

if ( $#argv > 1 ) then
   echo 'Usage :' $0 ccyymmddhh
   exit 1
else if ( $#argv == 0 ) then
   echo 'Usage :' $0 ccyymmddhh
   exit 1
endif


setenv runtime $1
setenv y4  `echo $runtime | cut -c1-4`
setenv y2  `echo $runtime | cut -c3-4`
setenv m2  `echo $runtime | cut -c5-6`
setenv d2  `echo $runtime | cut -c7-8`
setenv h2  `echo $runtime | cut -c9-10`

# go to system dir and set environment variables
#####################
cd /home/dataop/scripts-data/
source ./settings
setenv logfile "$logdir/gts.$runtime.log"
###########################################

# check data status
set check = `$downscriptdir/read-status.csh gts $runtime check`
if ( $check == 0 ) then
else
    $downscriptdir/modify-status.csh gts $runtime init
endif

# set $flag_download according to $force_download_gts and data status
msg_log "--------------------------------------------"
msg_log "BEG" `date`
msg_log "--------------------------------------------"
if ( $force_download_gts ) then
    set flag_download = 1
    msg_log "WARNING: FROCE-DOWNLOAD ACTIVATED"
else
    set flag = `$downscriptdir/read-status.csh gts $runtime status`
    if ( $flag == 0 ) then
        msg_log "GTS DATA $runtime WILL BE DOWNLOADED."
        set flag_download = 1
    else if ( $flag == 1 ) then
        msg_log "GTS DATA $runtime IS ALREADY DOWNLOADED."
        set flag_download = 0
    else if ( $flag == 9 ) then
        msg_log "GTS DATA $runtime IS BEING DOWNLOADED"
        set flag_download = 0
    endif
endif

# download
if ( $flag_download ) then
	msg_log "WRITTING DATA STATUS... 9"
	$downscriptdir/modify-status.csh gts $runtime 9
	set server  = "http://www.met.fsu.edu/rawdata"
	set local   = $gtsdir
	mkdir -p $local/${y4}${m2}
	cd $local/${y4}${m2}

    plog "DOWNLOADING GTS DATA FROM WEB SERVER..."
	msg_log "DOWNLOADING GTS DATA FROM WEB SERVER..."
    plog "$server"
	msg_log "$server"

    wget "$server/syn/$y4$m2$d2/$y4$m2$d2${h2}.syn"           -O "tmp.$y4$m2$d2${h2}.syn"    -qcN
    msg_log "$y4$m2$d2${h2}.syn Finished."
    wget "$server/aircraft/$y4$m2$d2/$y4$m2$d2${h2}.acrft"    -O "tmp.$y4$m2$d2${h2}.acrft"  -qcN
    msg_log "$y4$m2$d2${h2}.acrft Finished."
    wget "$server/dropsonde/$y4$m2$d2/$y4$m2$d2${h2}.drpsnd"  -O "tmp.$y4$m2$d2${h2}.drpsnd" -qcN
    msg_log "$y4$m2$d2${h2}.drpsnd Finished."
    wget "$server/pibal/$y4$m2$d2/$y4$m2$d2${h2}.pibal"       -O "tmp.$y4$m2$d2${h2}.pibal"  -qcN
    msg_log "$y4$m2$d2${h2}.pibal Finished."
    wget "$server/radiosonde/$y4$m2$d2/$y4$m2$d2${h2}.rdisnd" -O "tmp.$y4$m2$d2${h2}.rdisnd" -qcN
    msg_log "$y4$m2$d2${h2}.rdisnd Finished."
    wget "$server/metar/$y4$m2$d2/$y4$m2$d2${h2}.metar"       -O "tmp.$y4$m2$d2${h2}.metar"   -qcN
    msg_log "$y4$m2$d2${h2}.metar Finished."
    set localfile   = "gts_${y4}${m2}${d2}${h2}57"
    \rm -f $localfile $localfile.gz
    cat tmp.$y4$m2$d2${h2}.* >! $localfile
    \rm -rf tmp.$y4$m2$d2${h2}.*
    if ( "`wc -c $localfile | cut -d' ' -f1`" == 0 ) then
        \rm -rf $localfile
        plog "NO GTS RECORDS FOR $localfile, EXIT NOW!"
		msg_log "NO GTS RECORDS FOR $localfile . WRITTING DATA STATUS... 0"
        $downscriptdir/modify-status.csh gts $runtime 0
    endif
    msg_log "ZIPPING GTS FILE $localfile"
    gzip $localfile
    msg_log "SUCCESSFUL! WRITTING DATA STATUS... 1"
    $downscriptdir/modify-status.csh gts $runtime 1
endif
msg_log "--------------------------------------------"
msg_log "END" `date`
msg_log "--------------------------------------------"
exit 0

